<?php
class CmsCommentControl extends CmsBaseControl
{
    protected $fbUserId;
    protected $postId;
    protected $commentModel;

    public function CmsCommentControl($post_id)
    {
        $this->postId = $post_id;
        $this->commentModel = CmsCommentModel::GetInstance();
        parent::__construct();
    }

    protected function GetRefreshUrl()
    {
        return parent::GetRefreshUrl().FS.$this->postId;
    }

    public function Render()
    {
        $comment = $this->SaveComment(RequestHandler::GetPost('comment'));
        $list = new CmsCommentListControl($this->postId);

        $fb_connect = $this->GetFBConnectButton();
        $nameBox = $this->GetNameBox();

        $this->Replace('name_box', $nameBox);
        $this->Replace('fb_connect', $fb_connect);
        $this->Replace('facebook_apikey', SettingModel::Get('FACEBOOK_API_KEY'));
        $this->Replace($comment);
        $this->Replace('comment_list', $list->Render());
        $this->Replace('refresh_url', $this->GetRefreshUrl());
        return parent::Render();
    }

    protected function SaveComment($comment)
    {
        try
        {
            if ($comment)
            {
                $comment['post_id'] = $this->postId;
                $this->commentModel->Insert($comment);
            }
            return $this->commentModel->GetEmptyRecord();
        }
        catch (Exception $ex)
        {
            MessageBase::Warning($ex->getMessage());
            return $comment;
        }
    }

    protected function GetNameBox()
    {
        return '<p>
                    <label for="readerCommentName" class="">Name</label>
                    <input name="comment[name]" id="readerCommentName" value="[:name:]" maxlength="50" type="text">
                </p>
                <p>
                    <label for="readerCommentEmail" class="">E-mail</label>
                    <input name="comment[email]" id="readerCommentEmail" value="[:email:]" maxlength="70" type="text">
                </p>';
    }

    protected function GetFBConnectButton()
    {
        return '<p>Enter your personal information to the left, or sign in with your Facebook account by clicking the button below.</p>
            <p><a href="#" onclick="FB.Connect.requireSession(); return false;"><img id="fb_login_image" src="http://static.ak.fbcdn.net/images/fbconnect/login-buttons/connect_light_medium_long.gif" alt="Connect" /></a></p>';
    }

    protected function GetFBWelcomeMsg()
    {
        return "<div style='float:left; margin:10px'><fb:profile-pic uid='{$this->fbUserId}' linked='true' /></div><br><p>Welcome, Hoang Le. You are signed in with your Facebook account.</p><br>"
            .'<p>Click here to sign out</p>';
    }
}
?>